<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    .imgBox {
      margin: 1000px auto;
      width: 800px;
      height: 160px;
      overflow: hidden;
      background-color: #888;
    }

    .imgBox img {
      width: 100%;
      display: none;
    }
  </style>
</head>

<body>
  <div class="imgBox">
    <img src="" data-img="https://www.itcast.cn/images/newslide/homepageandphone/20224109184146299.jpg" alt="">
  </div>
  <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.0/jquery.min.js"></script>
  <script>
    const $imgBox = $('.imgBox');
    const $img = $imgBox.children('img');
    const $window = $(window);

    $window.on('load scroll', function () {
      if ($img.attr('isLoad') === 'true') {
        return;
      }
      // 【图片底边】到【页面顶端】的距离     => imgTop
      // 【浏览器底边】到【页面顶端】的距离     => winTop
      // winTop >= imgTop or imgTop <= winTop，说明图片完全进入屏幕可视区了
      const imgTop = $imgBox.outerHeight() + $imgBox.offset().top;
      const winTop = $window.outerHeight() + $window.scrollTop();

      if (imgTop <= winTop) {
        // 加载真实图片
        $img.attr('src', $img.data('img'));
        $img.on('load', function () {
          $img.stop().fadeIn();
        });
        $img.attr('isLoad', true); // attr 存储的自定义属性值都是字符串格式的
      }
    });
  </script>
</body>

</html>